<template>

  <div>
    <el-upload
      list-type="picture-card"
      action="#"
      :on-preview="preview"
      :on-change="changeFile"
      :on-remove="handleRemove"
      :before-upload="beforeUpload"
      :limit="9"
      :file-list="fileList"
      :class="{disabled:fileComputed}"
      :http-request="UploadFile"
      :disabled="isTrue"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <div ref="viewerContainer" style="display: none;">
      <img ref="viewerImage" :src="imgUrl">
    </div>
  </div>
</template>

<script>
import { UploadFile } from '@/api/user'
import Viewer from 'viewerjs'
import 'viewerjs/dist/viewer.css'
export default {
  props: {
    isTrue: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      imgUrl: '',
      fileList: [],
      currentFileUid: null
    }
  },
  computed: {
    fileComputed() {
      return this.fileList.length === 9
    }
  },
  beforeDestroy() {
    // Destroy viewer instance when the component is destroyed
    if (this.viewer) {
      this.viewer.destroy()
    }
  },
  methods: {
    // file是点击删除的文件
    handleRemove(file) {
      //   将原来的文件给排除掉了 剩下的就是最新的数组了
      this.fileList = this.fileList.filter(item => item.uid !== file.uid)
    },
    // 点击预览
    preview(file) {
      this.imgUrl = file.url
      this.$nextTick(() => {
        if (!this.viewer) {
          this.viewer = new Viewer(this.$refs.viewerContainer, {
            inline: false,
            button: true,
            navbar: false,
            title: false,
            toolbar: true,
            tooltip: true,
            movable: true,
            zoomable: true,
            rotatable: true,
            scalable: true,
            transition: true,
            fullscreen: true,
            keyboard: true,
            zIndex: 9999
          })
        } else {
          this.viewer.update()
        }
        this.viewer.show()
      })
    },
    // 上传成功之后 还会进来 需要实现上传代码的逻辑 这里才会成功
    changeFile(file, fileList) {
      this.fileList = fileList.map(item => item)
    },
    // // 做文件上传的检查了
    beforeUpload(file) {
      // 检查文件是否为img类型
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png', 'image/jpg']
      if (!types.some(item => item === file.type)) {
        // 如果不存在
        this.$message.error(this.$t('prompt.imgUpload'))
        return false
      }
      //  检查大小
      const maxSize = 15 * 1024 * 1024
      if (maxSize < file.size) {
        this.$message.error(this.$t('prompt.imgSize'))
        return false
      }
      this.currentFileUid = file.uid
      return true
    },
    // 上传
    async UploadFile(params) {
      const formData = new FormData()// 定义新的formData
      // 为formData赋值
      formData.append('file', params.file)
      // 发起上传请求
      const file = await UploadFile(formData)
      // 循环数组，为对应的filelist中的url替换
      this.fileList = this.fileList.map(item => {
        if (item.uid === this.currentFileUid) {
          return { url: file.file, upload: true }
        }
        return item
      })
    }
  }
}
</script>

<style>
    .disabled .el-upload--picture-card{
        display: none;
    }
    .showImg{
        width: 400px;
    }
</style>
